import { Request } from '../../../wxParse/request.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: getApp().globalData.statusBarHeight,
        TakeAwardManNum: [1, 2, 3], //
        all: true, //默认显示第一个分类
    },
    /**
     * 返回首页
     */
    gohome: function (param) {
        wx.navigateBack({
            delta: 1,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.OneMan = new Array(); //初始化 1-100人的区间范围
        this.TwoMan = new Array(); //初始化 101-1000人的区间范围
        this.ThreeMan = new Array(); //初始化 1001-10000人的区间范围
        new Request('Award/SelectAwardInfo').request(
            () => {
                return {
                    openid: app.globalData.openid, //用户openid
                };
            },
            (res) => {
                this.AwardList = res.data.data;
                this.AwardList.map(
                    function (e, n) {
                        if (e.ManId == 1) {
                            this.OneMan.push(e);
                        } else if (e.ManId == 2) {
                            this.TwoMan.push(e);
                        } else if (e.ManId == 3) {
                            this.ThreeMan.push(e);
                        }
                    }.bind(this)
                );

                //第一次加载是 第一个全部奖品
                this.setData({
                    AwardChooseListInfo: res.data.data,
                });
            }
        );
    },
    /**
     * 选择第一分类
     */
    _ChooseMan: function (param) {
        if (param.currentTarget.id == 1) {
            this.setData({
                AwardChooseListInfo: this.OneMan,
                one: true,
                two: false,
                three: false,
                all: false,
                buttonStatus: false, //点击选择奖品的时候才显示 点击按钮
            });
        } else if (param.currentTarget.id == 2) {
            this.setData({
                AwardChooseListInfo: this.TwoMan,
                one: false,
                two: true,
                three: false,
                all: false,
                buttonStatus: false, //点击选择奖品的时候才显示 点击按钮
            });
        } else if (param.currentTarget.id == 3) {
            this.setData({
                AwardChooseListInfo: this.ThreeMan,
                one: false,
                two: false,
                three: true,
                all: false,
                buttonStatus: false, //点击选择奖品的时候才显示 点击按钮
            });
        } else if (param.currentTarget.id == 4) {
            this.setData({
                AwardChooseListInfo: this.AwardList,
                one: false,
                two: false,
                three: false,
                all: true,
                buttonStatus: false, //点击选择奖品的时候才显示 点击按钮
            });
        }
    },
    /**
   * 选中 当前奖品 某一咧

   */
    _bindGoodIndex: function (param) {
        this.chooseAwardId = param.currentTarget.dataset.awardid; //奖品id
        this.setData({
            chooseGoods: param.currentTarget.dataset.index,
            buttonStatus: true, //点击选择奖品的时候才显示 点击按钮
        });
    },
    /**
     * 确定选择 此奖品
     */
    _buttonChooes: function (param) {
        if (this.data.one) {
            this.ArrayAwardChoose = this.OneMan;
        } else if (this.data.two) {
            this.ArrayAwardChoose = this.TwoMan;
        } else if (this.data.three) {
            this.ArrayAwardChoose = this.ThreeMan;
        } else if(this.data.all){
            this.ArrayAwardChoose = this.AwardList;
        }
        this.ArrayAwardChoose.map(
            function (e, n) {
                if (this.chooseAwardId == e.id) {
                    console.log(e);
                    const eventChannel = this.getOpenerEventChannel();
                    eventChannel.emit('acceptDataFromOpenedPage', {
                        awardid: this.chooseAwardId,
                        award_name: e.award_name,
                        award_image: e.award_image,
                        limit_EndNum: e.award_takepersonnumber, //满XXX人开奖
                        chooes_goodindex: this.data.chooseGoods,    //传入选择商品下标
                    });
                    return;
                }
            }.bind(this)
        );
        wx.navigateBack({
            delta: 1,
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var _that = this;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function(data) {
            console.log(data.data)
            if(data.data != null){
                _that.setData({
                    chooseGoods: data.data,
                    buttonStatus: true, //点击选择奖品的时候才显示 点击按钮
                });
            }
            
          })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
});
